package com.bw.test_bluetooth

import android.Manifest
import android.bluetooth.BluetoothAdapter
import android.util.Log
import androidx.annotation.RequiresPermission
import java.util.UUID

/**
 * @author ytx
 * @date 2025-10-30 14:13
 * @description
 */
class ServerThread(
    val adapter: BluetoothAdapter //本机蓝牙适配器
):Thread() {

    @RequiresPermission(Manifest.permission.BLUETOOTH_CONNECT)
    override fun run() {
        super.run()
        //服务端
        val bluetoothServerSocket =
            adapter.listenUsingInsecureRfcommWithServiceRecord(
                "aaaa",
                UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
            )
        while (true){
            val socket = bluetoothServerSocket.accept() //阻塞方法：等待客户端连接
            Log.d("ServerThread", "run:有人链接我了 ")
            //接受数据字符串
            val inputStream = socket.inputStream
            val bytes = ByteArray(1024)
            val length = inputStream.read(bytes)
            val string = String(bytes,0,length)
            Log.d("ServerThread", "run: 接收数据：$string")

        }
    }
}